﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoApp.EntityFrameworkCore;
using TodoApp.ResultModel;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TodoApp.TMS_Basics.TMS_coordination
{
    public class TMS_coordinations : DapperRepository<TodoAppDbContext>,ITransientDependency, ITMS_coordinations
    {
        public TMS_coordinations(IDbContextProvider<TodoAppDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 外协单位显示
        public async Task<PageResponse<TMS_coordinationDto>> GetCoo(string CooName, string CooPhone, int pageindex = 1, int pagesize = 5)
        {
            var result = new PageResponse<TMS_coordinationDto>();
            var dbConnection = await GetDbConnectionAsync();
            var sql = "select * from TMS_coordinationModel where 1=1 ";
            if (!string.IsNullOrEmpty(CooName))
            {
                sql += $" and CooName like '%{CooName}%'";
            }
            if (!string.IsNullOrEmpty(CooPhone))
            {
                sql += $" and CooPhone  like '%{CooPhone}%'";
            }

            var list = await dbConnection.QueryAsync<TMS_coordinationDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = list.ToList();
            result.PageTotal = list.Count();
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only ";
            var quer = await dbConnection.QueryAsync<TMS_coordinationDto>(sql, transaction: await GetDbTransactionAsync());
            result.PageData = quer.ToList();
            return result;

        }
        #endregion


        #region 外协单位添加
        public async Task<int> CooAdds(TMS_coordinationDto info)
        {

            var dbConnection = await GetDbConnectionAsync();

            info.Id = Guid.NewGuid();

            var list = await dbConnection.ExecuteAsync($"insert into TMS_coordinationModel values ('{info.Id}','{info.CooName}','{info.CooPhone}','{info.CooEml}','{info.CooDss}','{info.CooNum}','{info.CooTxt}')", null,
                 await GetDbTransactionAsync());
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        #endregion

        #region 外协删除
        public async Task<int> DeletCoo(string Id)
        {

            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync($"delete from TMS_coordinationModel where Id='{Id}'", null,
                 await GetDbTransactionAsync());

        }

        #endregion

    }
}
